/**
 * RenweiWu
 * PubTerminal.java
 * 2017年8月26日
 * PubTerminal
 * TODO
 * 
 */
package com.etrans.web.gps.business.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.domain.PubTerminal;
import com.etrans.web.gps.business.base.service.PubTerminalSevice;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

/**
 * @Description:车载终端控制器
 * @author RenweiWu
 * @date 2017年8月26日
 * @ClassName:  PubTerminal  
 * @param:   
 */

@Controller
@RequestMapping("/base/PubTerminal/")
public class PubTerminalContorller extends BaseController{
	
	@Autowired
	private PubTerminalSevice pubTerminalSevice;
	
	
	@RequestMapping("toPubTerminal.html")
	public String toPubTerminal(){
		return "views/business/base/terminal/pubterminal";
	}

	@RequestMapping("toaddPubterminal.html")
	public String toaddPubterminal(Model modle,String id){
		if(id!=null&&!"".equals(id)){
			PubTerminal vo =pubTerminalSevice.queryOneById(Integer.parseInt(id));
			modle.addAttribute("vo",vo);
			modle.addAttribute("action","update");
		}else{
			modle.addAttribute("action","save");
		}
		return "views/business/base/terminal/addpubterminal";
	}
	
	@RequestMapping("addPubterminal.html")
	public @ResponseBody Map<String,Object> addPubterminal(PubTerminal terminal){
		Map<String,Object> map = new HashMap<String,Object>();
		if(pubTerminalSevice.addPubTerminal(terminal)){
			map.put("code",0);
			map.put("msg","添加成功");
		}else {
			map.put("code",1);
			map.put("msg","添加失败");
		}
		return map;
	}
	@RequestMapping("queryPubTerminal.html")
	public @ResponseBody PageVo queryPubTerminal(String paramsGrid){
		Map<String,Object> map = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		map.put("Startpage",((int)map.get("page")-1)*((int)map.get("pageSize")));
		return pubTerminalSevice.queryPubTerminal(map);
	}
	
	
	@RequestMapping(value="updatePubTerminal.html")
	public @ResponseBody Map<String,Object> updatePubTerminal(PubTerminal terminal){
		Map< String,Object> map = new HashMap<String, Object>();
		if(pubTerminalSevice.updatePubTerminal(terminal)){
			map.put("code", 0);
		}else {
			map.put("code", 1);
		};
		return map;
	}
	
	@RequestMapping("deletePubTerminal.html")
	public @ResponseBody Map<String,Object> deleteOrganzation(String id){
		Map<String,Object> map =new HashMap<String,Object>();
		if(id!=null&&!"".equals(id)){
			if(pubTerminalSevice.deletepubTerminal(Integer.parseInt(id))){
				map.put("code", 0);
			}else {
				map.put("code", 1);
			};
		}else {
			map.put("code", 1);
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportPubTerminal.html")
	public void exportRecord( HttpServletRequest request, HttpServletResponse response,@RequestBody String paramsGrid, String colModel, String title, String StartPage, String EndPage, String pagesize) {
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("Startpage",Integer.parseInt(StartPage)-1);
		param.put("pageSize",(Integer.parseInt(EndPage) - Integer.parseInt(StartPage)+1)*Integer.parseInt(pagesize));
		paramMap.putAll(param);
		PageVo pageVo = pubTerminalSevice.queryPubTerminal(paramMap);
		List<PubTerminal> list = (List<PubTerminal>)pageVo.getRows();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
		int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
		if(end>(list.size()-1)){
			end=list.size();
		}
		for(int i=start;i<end;i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			PubTerminal obj=(PubTerminal)list.get(i);
			map.put("name", obj.getName()==null ? "" : obj.getName());
			map.put("applyKind", obj.getApplyKind()==null ? "" : obj.getApplyKind());
			map.put("locationModule", obj.getLocationModule()==null ? "" : obj.getLocationModule());
			map.put("commMode", obj.getCommMode()==null ? "" : obj.getCommMode());
			map.put("venderName", obj.getVenderName()==null ? "" : obj.getVenderName());
			map.put("venderCode", obj.getVenderCode()==null ? "" : obj.getVenderCode());
			rows.add(map);
		}
		int index=-1;
		@SuppressWarnings("rawtypes")
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
	
}
